<template>
  <view v-if="goodsInfo.goods_name" class="goods-detail-container">
    <!-- 轮播图区域 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" circular="true">
      <swiper-item v-for="(item,index) in goodsInfo.pics" :key="index">
        <image :src="item.pics_big" @click="previw(index)"></image>
      </swiper-item>
    </swiper>
    <!-- 商品信息区域 -->
    <view class="goods-info-box">
      <!-- 商品价格 -->
      <view class="goods-price">￥{{goodsInfo.goods_price}}</view>
      <!-- 商品主题区域 -->
      <view class="goods-body">
        <!-- 商品的名字 -->
        <view class="goods-name">{{goodsInfo.goods_name}}</view>
        <!-- 收藏 -->
        <view class="goods-favi">
          <uni-icons type="star-filled" size="17"></uni-icons>
          <text>收藏</text>
        </view>
      </view>
      <!-- 商品运费 -->
      <view class="goods-yf">
        快递：免运费
      </view>
      <!-- 富文本 -->
      <rich-text :nodes="goodsInfo.goods_introduce"></rich-text>
      <!-- 商品导航组件区域 -->
      <view class="goods-nav">
        <uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick"
          @buttonClick="buttonClick" />
      </view>
    </view>
  </view>
</template>

<script>
  import {
    mapState,
    mapMutations,
    mapGetters
  } from 'vuex'
  export default {
    computed: {
      ...mapState('my_cart', []),
      ...mapGetters('my_cart', ['total'])
    },
    watch: {
      // 使用普通函数的形式定义的 watch 侦听器，在页面首次加载后不会被调用。因此导致了商品详情页在首次加载完毕之后，不会将商品的总数量显示到商品导航区域：
      // total(newVal) {
      //   const findResult = this.options.find(g => g.text === '购物车')
      //   if (findResult) {
      //     findResult.info = newVal
      //   }
      // }
      // 定义 total 侦听器，指向一个配置对象
      total: {
        // handler 属性用来定义侦听器的 function 处理函数
        handler(newVal) {
          const findResult = this.options.find(g => g.text === '购物车')
          if (findResult) {
            findResult.info = newVal
          }
        },
        // immediate 属性用来声明此侦听器，是否在页面初次加载完毕后立即调用
        immediate: true
      }
    },
    data() {
      return {
        options: [{
          icon: 'shop',
          text: '店铺',
          infoBackgroundColor: '#007aff',
          infoColor: "red"
        }, {
          icon: 'cart',
          text: '购物车',
          info: 0
        }],
        buttonGroup: [{
            text: '加入购物车',
            backgroundColor: '#ff0000',
            color: '#fff'
          },
          {
            text: '立即购买',
            backgroundColor: '#ffa200',
            color: '#fff'
          }
        ],
        //商品详情
        goodsInfo: {}
      };
    },
    onLoad(options) {
      const goods_id = options.goods_id
      this.getGoodsInfo(goods_id)
    },
    methods: {
      ...mapMutations('my_cart', ['addToCart']),
      async getGoodsInfo(goods_id) {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/detail', {
          goods_id
        })
        if (res.meta.status !== 200) return uni.$showMsg()
        res.message.goods_introduce = res.message.goods_introduce.replace(/<img/g, '<img style="display:block;" ')
          .replace(/webp/g, 'jpg')
        this.goodsInfo = res.message
      },
      previw(index) {
        uni.previewImage({
          current: index,
          urls: this.goodsInfo.pics.map(x => x.pics_big)
        })
      },
      //点击底部导航购物车按钮跳转到购物车页面
      onClick(e) {
        if (e.content.text === '购物车') {
          uni.switchTab({
            url: '/pages/cart/cart'
          })
        }
      },
      //点击底部导航加入购物车按钮
      buttonClick(e) {
        if (e.content.text === '加入购物车') {
          //组织商品的信息对象
          //调用addTocart方法
          //{ goods_id, goods_name, goods_price, goods_count, goods_small_logo, goods_state }
          const goods = {
            goods_id: this.goodsInfo.goods_id, // 商品的Id
            goods_name: this.goodsInfo.goods_name, // 商品的名称
            goods_price: this.goodsInfo.goods_price, // 商品的价格
            goods_count: 1, // 商品的数量
            goods_small_logo: this.goodsInfo.goods_small_logo, // 商品的图片
            goods_state: true // 商品的勾选状态
          }
          this.addToCart(goods)
        }
      }
    }
  }
</script>

<style lang="scss">
  swiper {
    height: 750rpx;

    image {
      width: 100%;
      height: 100%;
    }
  }

  .goods-info-box {
    padding: 10px 0 10px 10px;

    .goods-price {
      color: #C00000;
      font-size: 18px;
      margin: 10px 0;
    }

    .goods-body {
      display: flex;
      justify-content: space-between;

      .goods-name {
        font-size: 13px;
        margin-right: 10px;
      }

      .goods-favi {
        width: 120px;
        font-size: 12px;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        border-left: 1px solid #efefef;
        color: grey;
      }
    }

    .goods-yf {
      font-size: 12px;
      color: grey;
      margin: 10px 0;
    }
  }

  .goods-nav {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
  }

  .goods-detail-container {
    padding-bottom: 50px;
  }
</style>